package com.bawan.vims.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * <p> Description	: 系统属性文件工具类 </p>
 * <p> Copyright	: Copyright (c) 2015 by FITT</p>
 * <p> Author 		: ZCC </p>
 * <p> Create		: 2015年1月30日 上午9:50:49 </p> 
 * <p> version 		: 1.0 </p>
 */
public class PropertyUtil {
    private static Logger log = Logger.getLogger("vims");

    /**
     * 定义内部私有静态类,拥有一个私有的静态常量,指定PropertyUtil的实例
     * 单例类 
     */
    private static class PropertyUtilHolder{
    	private static final PropertyUtil INSTANCE = new PropertyUtil();
    }
    /**
     * 私有化构造, 以实现单例
     */
    private PropertyUtil(){}
    
    /**
     * 获得实例
     * @return
     */
    public static PropertyUtil getInstance() {
        init();
        return PropertyUtilHolder.INSTANCE;
    }

    /**
     * 定义存放属性值的缓存变量
     */
    private static Properties properties = new Properties();
    
    /**
     * 初始化
     */
    private static void init() {
        InputStream in = null;
        try {
        	String propertyFilePath = Thread.currentThread().getContextClassLoader().getResource("application.properties").getPath();
            in = new FileInputStream(propertyFilePath);

            properties.clear();
            properties.load(in);
        } catch (IOException ioe) {
            log.log(Level.SEVERE, "PropertyUtil.init() occurred error：" + ioe.getMessage(), ioe);
        } finally {
        	try {
				if (in != null) {
					in.close();
					in = null;
				}
			} catch (IOException e) {}
        }

    }
    
    /**
     * 根据Key获得属性值Value
     * @param key
     * @return
     */
    public String getProperty(String key) {
    	String value = properties.getProperty(key);
    	if (value == null || value.trim().length() <= 0) {
    		log.log(Level.INFO, "无法找到key[" + key + "]对应的值");
    	}
        return value;
    }

    /**
     * 将Key与对应的Value写入属性文件
     * @param key
     * @param value
     */
    public void setProperty(String key, String value) {
    	 OutputStream out = null;
        try {
        	String propertyFilePath = Thread.currentThread().getContextClassLoader().getResource("application.properties").getPath();
            out = new FileOutputStream(propertyFilePath);
            
            properties.setProperty(key, value);
            properties.store(out, "Update" + key + " value");
            //重新加载属性
            init();
        } catch (IOException ioe) {
        	log.log(Level.SEVERE, "PropertyUtil.setProperty() occurred error：" + ioe.getMessage(), ioe);
        }
    }
    
    public static void main(String[] args) {
		System.out.println(PropertyUtil.getInstance().getProperty("jdbc.password"));
		
		new Timer().scheduleAtFixedRate(new CustomTimerTask(), 3000, 3000);
	}
    
    static class CustomTimerTask extends TimerTask {
    	@Override
		public void run() {
    		System.out.println(PropertyUtil.getInstance().getProperty("jdbc.password"));
		}
    }
}